package cn.gailvlun.gll.presentation.login.viewmodel

import cn.gailvlun.gll.databinding.ActivityForgotPasswordBinding
import cn.gailvlun.gll.net.HttpMethods
import cn.gailvlun.gll.net.HttpSubscriber
import cn.gailvlun.gll.net.base.Profile
import cn.gailvlun.gll.net.login.ForgotPasswordReq
import cn.gailvlun.gll.net.login.SendForgotPasswordCaptchaReq
import cn.gailvlun.gll.net.login.SendForgotPasswordCaptchaRes
import cn.gailvlun.gll.presentation.base.BaseActivity
import cn.gailvlun.gll.presentation.login.LoginActivity
import cn.gailvlun.gll.presentation.login.adapter.LoginType
import cn.gailvlun.gll.presentation.main.MainActivity
import cn.gailvlun.gll.util.*
import com.blankj.utilcode.util.SPUtils


class ForgotPasswordViewModel(val activity: BaseActivity, private val mBinding: ActivityForgotPasswordBinding) : BaseViewModel(activity) {

    fun onFinish() {
        val code = mBinding.cetCaptcha.text.toString().trim()
        val cc = countryNameAndCode.split(" ")[1]

        val phoneNumber = mBinding.cetPhone.text.toString().trim()
        val newPwd = mBinding.petNewPassword.text.toString().trim()



        if (IsEmpty.string(mBinding.cetPhone.text.toString().trim())) {
            ToastUtil.showShort("请填写手机号")
            return
        }






        if (IsEmpty.string(code)) {
            ToastUtil.showShort("验证码不能为空")
            return
        }

        if (IsEmpty.string(newPwd)) {
            ToastUtil.showShort("请填写密码")
            return
        }

        if (newPwd.length < 6) {
            ToastUtil.showShort("密码不能小于6位")
            return
        }
        if (newPwd.length > 16) {
            ToastUtil.showShort("密码不能多于16位")
            return
        }

        val phoneNumberWithCc = "$cc$phoneNumber"

        val forgotPasswordReq = ForgotPasswordReq()
        forgotPasswordReq.mobile = phoneNumberWithCc
        forgotPasswordReq.verification_code = code
        forgotPasswordReq.password = newPwd

        val pd = DialogUtil.showIndeterminate(activity)
        HttpMethods.getLoginService()
                .forgotPassword(forgotPasswordReq)
                .compose(RxUtil.applyScheduler())
                .subscribe(object : HttpSubscriber<Profile>() {
                    override fun onSuccess(response: Profile?) {
                        pd.dismiss()
                        ToastUtil.showShort("重置密码成功")
                        ProfileUtil.save(response)
                        val profile = ProfileUtil.getProfile()
                        if (profile != null && profile.token != null &&
                                profile.refresh_token != null &&
                                profile.is_mobile_bound &&
                                profile.isActivated) {
                            MainActivity.openActivity(activity, false, true)
                        } else {
                            LoginActivity.openActivity(activity, true)
                        }
                    }

                    override fun onFailure(errMsg: String?, profile: Profile?, code: Int) {
                        pd.dismiss()
                        DialogUtil.showError(activity, errMsg)
                    }
                })


    }

    fun onCaptcha() {

        if (IsEmpty.string(mBinding.cetPhone.text.toString().trim())) {
            ToastUtil.showShort("请填写手机号")
            return
        }
        mBinding.tvCaptcha.startCount()
        val cc = countryNameAndCode.split(" ")[1]
        val phoneNumberWithCc = "$cc${mBinding.cetPhone.text.toString().trim()}"
        sendCode(phoneNumberWithCc)
    }


    private fun sendCode(phoneNumberWithCc: String) {
        val sendForgotPasswordCaptchaReq = SendForgotPasswordCaptchaReq()
        sendForgotPasswordCaptchaReq.mobile = phoneNumberWithCc
        val pd = DialogUtil.showIndeterminate(activity)
        HttpMethods.getLoginService()
                .sendForgotPasswordCaptcha(sendForgotPasswordCaptchaReq)
                .compose(RxUtil.applyScheduler())
                .subscribe(object : HttpSubscriber<SendForgotPasswordCaptchaRes>() {
                    override fun onSuccess(response: SendForgotPasswordCaptchaRes?) {
                        pd.dismiss()
                        SPUtils.getInstance().put("loginType", LoginType.phone.name)
                        ToastUtil.showShort("验证码已发送，请查收短信")
                    }

                    override fun onFailure(errMsg: String?, response: SendForgotPasswordCaptchaRes?, code: Int) {
                        pd.dismiss()
                        DialogUtil.showError(activity, errMsg)
                        mBinding.tvCaptcha.stopCount()
                    }
                })
    }
}